package io.rong.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yunshipei.enterplorer.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import io.rong.app.model.GroupCreateInfo;
import io.rong.app.utils.StringUtilSub;


/**
 * Created by Bob on 2015/1/31.
 */
public class GroupListAdapter extends BaseAdapter {
    private static final String TAG = GroupListAdapter.class.getSimpleName();
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<GroupCreateInfo> mResults;
    private ArrayList<View> mViewList;

    OnItemButtonClick mOnItemButtonClick;

    public void setOnItemButtonClick(OnItemButtonClick onItemButtonClick) {
        this.mOnItemButtonClick = onItemButtonClick;
    }


    public GroupListAdapter(Context context, List<GroupCreateInfo> result) {

        this.mResults = removeDuplicate(result);
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mViewList = new ArrayList<View>();
    }

    //去重
    private List<GroupCreateInfo> removeDuplicate(List<GroupCreateInfo> list) {
        if (list == null)
            return null;
        HashSet hashSet = new HashSet(list);
        list.clear();
        list.addAll(hashSet);
        return list;

    }

    public void clear() {
        if (mResults != null) {
            mResults.clear();
        }
    }

    public void addCollection(Collection<GroupCreateInfo> createInfos) {
        mResults.addAll(createInfos);
    }

    @Override
    public int getCount() {
        return mResults.size();

    }

    @Override
    public GroupCreateInfo getItem(int position) {
        return mResults.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = mLayoutInflater.inflate(R.layout.de_item_group, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.mGroupName = (TextView) convertView.findViewById(R.id.group_adaper_name);
            viewHolder.mGroupCurrentNum = (TextView) convertView.findViewById(R.id.group_current_num);
            viewHolder.mGroupCurrentSum = (TextView) convertView.findViewById(R.id.group_current_sum);
            viewHolder.mGroupLastmessge = (TextView) convertView.findViewById(R.id.group_last_mess);
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.group_adapter_img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (viewHolder != null) {
            viewHolder.mGroupName.setText(StringUtilSub.getInstance().SubStringAddCH(0, mResults.get(position).getName(), 10));
            viewHolder.mGroupCurrentNum.setText(mResults.get(position).getNumber() + "/");
            viewHolder.mGroupCurrentSum.setText(mResults.get(position).getMax_number() + "");
            viewHolder.mGroupLastmessge.setText(mResults.get(position).getIntroduce());
            Glide.with((Activity) mContext).load(mResults.get(position).getPortraitUri()).dontAnimate().placeholder(R.drawable.group).into(viewHolder.mImageView);
        }

        return convertView;
    }

    public interface OnItemButtonClick {
        public boolean onButtonClick(int position, View view);

    }

    static class ViewHolder {
        TextView mGroupName;
        TextView mGroupCurrentNum;
        TextView mGroupCurrentSum;
        TextView mGroupLastmessge;
        ImageView mImageView;
    }
}
